/*
 *
 *  Copyright (C) 2010 Tal Eisenberg
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package tal.drivechain.chain.shell;

import java.util.Arrays;
import java.util.Comparator;
import java.util.TreeSet;
import tal.drivechain.chain.script.ScriptCommand;
import tal.drivechain.plugin.Module;
import tal.drivechain.system.Globals;
import tal.drivechain.system.PluginLoader;
import tal.drivechain.variable.types.DataType;

/**
 *
 * @author eisental
 */
public class AboutCommand extends ShellCommand {
    public AboutCommand(DriveShell shell) { super(shell); }

    @Override
    public boolean runCommand(ScriptCommand command) throws IllegalArgumentException {
        if (command.getName().equals("about")) {
            if (command.getArg1().equals("module")) {
                String name = command.getArg2();
                Module mod = PluginLoader.getModule(name);
                out.println(InfoPrinter.printModuleAbout(mod));
            } else if (command.getArg1().equals("datatypes")) {
                out.println("The following datatypes are available:");
                for (DataType d : DataType.types)
                    out.println(" - " + d.getName() + " (class: " + d.getValueClass().getName() + ")");
            } else if (command.getArg1().equals("shell")) {

                out.println("driveshell running on " + Globals.VERSION.toString(true, true, true, true));
                out.println("\nAvailable commands are:");

                TreeSet<ShellCommand> sorted = new TreeSet(new Comparator() {

                    public int compare(Object o1, Object o2) {
                        ShellCommand first = (ShellCommand)o1;
                        ShellCommand second = (ShellCommand)o2;
                        String firstC, secondC;
                        if (first instanceof ShellObject) firstC = ((ShellObject)first).name;
                        else firstC = first.getClass().getSimpleName();

                        if (second instanceof ShellObject) secondC = ((ShellObject)second).name;
                        else secondC = second.getClass().getSimpleName();

                        return firstC.compareTo(secondC);
                    }
                });
                sorted.addAll(Arrays.asList(shell.getShellCommands()));

                for (ShellCommand c : sorted) {
                    String line = " - " + c.getName() + ": ";
                    for (String cmd : c.getCommands())
                        line += cmd +" / ";
                    out.println(line.substring(0, line.length()-3));
                }

                out.println("\nShell shortcuts:");
                ShortcutObject.printShortcuts(shell);
            } else if (command.getArg2().equals("modules")) {
                out.println(InfoPrinter.printAvailableModules(command.getArg1()));
            } else if (command.getArg1().equals("modules")) {
                out.println(InfoPrinter.printAvailableModules(null));
            } else throw new IllegalArgumentException("Unknown about object: " + command.getArg1());
            return true;
        } else return false;
    }

    @Override
    public String[] getCommands() {
        return new String[] { "about module <class-name>", "about <package-name> modules", "about shell", "about datatypes" };
    }

    @Override
    public String getName() {
        return "about";
    }

}
